var $ = require('../../lib/jquery');
var util = require('./common/util');
var nav = require('./common/nav');
var _header = require('./common/header');
var dataArr = require('./assist/zytzbs');
var template =require('../tpl/template');
var minHeight = require('./common/minHeight');
minHeight.init();
console.log(dataArr);
var _nav = new nav();
var header = new _header();
// 中医体质辨识
var zytz_bs = {
  page: 1,
  init: function (medicalid) {
    var arr = new Array();
    arr.length =33;
    this.arr  = arr;
    this.render();
    this.bindevent()
    this.maxpage = dataArr.length;
  },
  render: function (medicalid) {
    var tpl = template('zy_tzbs', {
      data: dataArr
    });
    $('.js_next_page').before(tpl);
  },
  show: function (medicalid) {
    this.hide();
    var index = this.page-1;
    $('.js_content').find('.class' + index).removeClass('hide');
  },
  hide: function (medicalid) {
    $('.js_content').find('.wrap').addClass('hide');
  },
  verify: function (medicalid) {
    var arr = dataArr[(this.page-1)];
    for(var i=0;i<arr.length;i++){
      if (!arr[i].core){
        return arr[i].index;
      }
    }
    return '';
  },
  calculate: function (medicalid) { // 计算分数
    var qixu = this.arr[1] + this.arr[2] + this.arr[3] + this.arr[13];
    var yangxu = this.arr[10] + this.arr[11] + this.arr[12] + this.arr[28];
    var yinxu = this.arr[9] + this.arr[20] + this.arr[25] + this.arr[30];
    var yanshi = this.arr[8] + this.arr[15] + this.arr[27] + this.arr[31];
    var shire = this.arr[22] + this.arr[24] + this.arr[26] + this.arr[29];
    var xueyu = this.arr[18] + this.arr[21] + this.arr[23] + this.arr[32];
    var qiyu = this.arr[4] + this.arr[5] + this.arr[6] + this.arr[7];
    var teb = this.arr[14] + this.arr[16] + this.arr[17] + this.arr[19];
    var pinghe = this.arr[0] + (6-this.arr[1]) + (6-this.arr[3]) + (6-this.arr[4]) + (6-this.arr[12]);
    if (pinghe >= 17 && qixu <= 10 && yangxu <= 10 && yinxu <= 10 && yanshi <= 10 && shire <= 10 && xueyu <= 10 && qiyu <= 10 && teb<=10 ) {
      return 0;
    }else {
      if (qixu >= 11) {
        return 1;
      } else if (yangxu >= 11) {
        return 2;
      } else if (yinxu >= 11) {
        return 3;
      } else if (yanshi >= 11) {
        return 4;
      } else if (shire >= 11) {
        return 5;
      } else if (xueyu >= 11) {
        return 6;
      } else if (teb >= 11) {
        return 7;
      } else if (qiyu >= 11) {
        return 8;
      } else if (qixu == 9 || qixu == 10){
        return 1;
      } else if (yangxu == 9 || yangxu==10){
        return 2
      } else if (yinxu == 9 || yinxu ==10){
        return 3;

      } else if (yanshi == 9 || yanshi==10){
        return 4;

      } else if (shire == 9 || shire==10){
        return 5;

      } else if (xueyu == 9 || xueyu==10){
        return 7;

      } else if (teb == 9 || teb==10){
        return 8;
      } else if (qiyu == 9 || qiyu==10){
        return 8;
      }else{
        console.error('基本上没有服务的，看来我们还需增加体质维护了');
        return 1;
      }

    }
    
  },
  bindevent: function (medicalid) {
    var _this = this;
    $('.js_index').on('click',function(){
      location.href="./index.html";
    })
    $('.js_next_page').on('click', function (){
      if (_this.verify()){
          alert('请先回答完第' + _this.verify() + '题');
        return true;
      }
      if (_this.page >= _this.maxpage) { // 这里应该是确认并提交 计算分数；
        var n = _this.calculate();
        location.href = './bsjg.html?answer=' + n;
      }else{
        _this.page ++;
        if (_this.page == _this.maxpage){
          $(this).find('span').text('确认提交');
        }
        _this.show();
      }
    })
    $('.js_content').on('click', '.js_select',function(){
      $(this).siblings().removeClass('on');
      $(this).addClass('on');
      var index = $(this).closest('.row').data().parentid;
      var _index =$(this).closest('.row').index();
      var core = $(this).data().core +1 ; // 分数
      dataArr[(_this.page-1)][_index].core = true;
      _this.arr[(index-1)] = core;
    })
  }
}
zytz_bs.init();